import { createRouter, createWebHashHistory } from "vue-router";
import type { RouteRecordRaw } from "vue-router";

import localCache from "@/utils/cache";
import { firstMenu } from "@/utils/map-menus";

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const routes: RouteRecordRaw[] = [
  {
    path: "/",
    redirect: "/main"
  },
  {
    path: "/login",
    name: "login",
    component: () => import("@/views/login/login.vue")
  },
  {
    path: "/main",
    name: "main",
    component: () => import("@/views/main/main.vue")
    // children: [] -> 根据userMenus来决定 -> children
  },
  {
    path: "/:pathMatch(.*)*",
    name: "notFound",
    component: () => import("@/views/not-found/not-found.vue")
  }
];

const router = createRouter({
  routes,
  history: createWebHashHistory()
});

// 导航守卫
router.beforeEach((to) => {
  if (to.path !== "/login") {
    const token = localCache.getCache("token");
    if (!token) {
      return "/login";
    }
  }
  if (to.path === '/main') {
    return firstMenu.url
  }
});

export default router;
